package y2015.date0914pm.DOM解析;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.Iterator;

public class DomParser {

    public void parse(String file) throws DocumentException {
        SAXReader domparser = new SAXReader();//创建解析器
        Document doc = domparser.read(file);//使用解析器解析，结果全部存入doc中
        Element root = doc.getRootElement();

        Iterator musicIterator = root.elementIterator();

        while (musicIterator.hasNext()) {
            Element music = (Element) musicIterator.next();
            Iterator attrIterator = music.attributeIterator();
            while (attrIterator.hasNext()) {
                Attribute arr = (Attribute) attrIterator.next();
                System.out.println("------------");
                System.out.println(arr.asXML());
                if ("id".equals(arr.getName())) {
                    System.out.println("id：" + arr.getValue());
                }
            }
        }
    }
}

